home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Utilities / PPC / mcvertPPC / mcvert.c < prev    next >
Text File  |  1998-04-08  |  46KB  |  1,446 lines

  1. /**
  2.  * mcvert.c - version 1.05 - 10 January, 1990
  3.  * Written by Doug Moore - Rice University - dougm@rice.edu - April '87
  4.  
  5.  * Sun bug fixes, assorted stuff - Jim Sasaki, March '89
  6.  
  7.  * Changed default max_line_size from 2000 to unlimited -
  8.  *                                           Doug Moore, April, '89
  9.  
  10.  * Sun 3/60 doesn't like odd-sized structs.  Bug fixed - Doug Moore, April, '89
  11.  *                                              - aided by Spencer W. Thomas
  12.  
  13.  * Didn't handle properly many hqx files combined in one file.  Bug fixed -
  14.  *                                           Doug Moore, June, '89
  15.  
  16.  * Modified to handle MacBinaryII specification. Jim Van Verth, Sept, '89
  17.  
  18.  * Fixed a bug when there are blank lines in hqx data, as happens when newline
  19.  * get translated to CRLF and then to \n\n, common for some file transfers.
  20.  * The last hqx line would be lost if the previous line was blank or junk.
  21.  *    Glenn Trewitt, Stanford University, 1990    (1.05)
  22.  
  23.  * Fixed a bug that occurred when extracting data or resource
  24.  * forks on a Sun 4.  It was a byte alignment problem.
  25.  * Rick Zaccone, zaccone@bucknell.edu.  April 1991.  Version 1.6
  26.  
  27.  * Fixed:
  28.  *   Sent all "Converting ... " lines to stdout instead of stderr
  29.  *   Changed mactypes.h for HP-UX systems
  30.  *      Alan Danziger, aland@cs.brandeis.edu.  October 1991. Version 1.6.5
  31.  
  32.  * ----------------------------------------------------------------------------
  33.  * External
  34.  * -----
  35.  * Fixed buffering bug when converting very small MacBinary files to hqx files.
  36.  * Provide helpful usage line.
  37.  * Control "Converting ... " lines separately with -S flag.
  38.  * Make encoding and decoding consistent by ignoring locked and init flags.
  39.  * Clean up some error messages; check for more errors; provide errno on error.
  40.  * Updated the man page.
  41.  * -----
  42.  * Internal
  43.  * -----
  44.  * Reformat source (sorry, local standard used by tools is tab space == 3)
  45.  * Remove compiler warning messages.
  46.  * Rename some variables.
  47.  * Added some comments to code.
  48.  * Added some offsets to struct definitions.
  49.  * Since the makefile has compilation flags,
  50.  *    make the compiles depend on the Makefile.
  51.  * -----
  52.  * Thanks to all who have gone before for creating, maintaining,
  53.  * improving, and providing this program and documentation.
  54.  * -----
  55.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  56.  * {Jskud@std.mentorg.com,Joseph_Skudlarek@mentorg.com}
  57.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  58.  * Version 1.70 09Jul92
  59.  * ----------------------------------------------------------------------------
  60.  
  61.  * ----------------------------------------------------------------------------
  62.  * External
  63.  * -----
  64.  * Added -V (Verbose) option (includes debugging information).
  65.  * Fixed bug converting hqx to MacBinary if last line is ":".
  66.  * Avoided a silent error and quick exit situation.
  67.  * -----
  68.  * Internal
  69.  * -----
  70.  * Got rid of almost all lint (SunOS and HP-UX) error messages.
  71.  * Compiled on SunOs, HP-UX, DomainOS.
  72.  * Incorporated Parag Patel <parag@netcom.com> changes for AU/X.
  73.  *    Here's some diffs for really quick cheap hacks to get mcvert to compile
  74.  *    and run under A/UX.  The main problem was that timeb does not exist, so
  75.  *    I added 2 #ifdef TIMEVAL to use the System-V timeval package instead.
  76.  *    The Makefile just has a -DTIMEVAL and a magic -U_SYSV_SOURCE to get
  77.  *    around a pre-defined type "ulong" in sys/types.h (thanks to Apple).
  78.  * Did more code overhauling:
  79.  *    add lots more comments, rename variables, reformat source.
  80.  * Put code in un_hqx to avoid suspected buffering problem.
  81.  * -----
  82.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  83.  * (503) 685-1576 (work)
  84.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  85.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  86.  * Version 1.80 15Jul92
  87.  * ----------------------------------------------------------------------------
  88.  
  89.  * ----------------------------------------------------------------------------
  90.  * External
  91.  * -----
  92.  * Made hqx file scan processing much smarter
  93.  *    so, for example, info-mac/comm/qwk-reader.hqx,
  94.  *    complete with extraneous colons in column one, converts correctly
  95.  *    (problem described by Edward John Sabol <es2j+@andrew.cmu.edu>)
  96.  * Avoid silly perror on usage message (prompted by Edward John Sabol)
  97.  * Improve error message regarding improper format
  98.  * Added more caveats to man page
  99.  * -----
  100.  * Internal
  101.  * -----
  102.  * Fixed typo's in printf lines to pass all expected arguments
  103.  *    (pointed out by Bo Holst-Christensen
  104.  *    [holst@diku.dk/dikubhc1@uts.uni-c.dk/holst@login.dkuug.dk])
  105.  * Tweak Makefile to ease shar creation and special case ulong, not A/UX
  106.  * Add yet more comments and debugging code
  107.  * -----
  108.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  109.  * (503) 685-1576 (work)
  110.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  111.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  112.  * Version 1.82 30Jul92
  113.  * ----------------------------------------------------------------------------
  114.  
  115.  * ----------------------------------------------------------------------------
  116.  * External
  117.  * -----
  118.  * relax exactly 64 characters per incoming hqx file, and
  119.  * handle files without trailing newline, so, eg, 
  120.  *    Telnet2.5docs.sit.hqx now converts correctly
  121.  *       (failure reported by Justin Sullivan <justin@f2.facts.uky.edu>)
  122.  *    now also processes info-mac/app/road-map.hqx correctly
  123.  *       (failure reported by Victor Norton<norton@andy.bgsu.edu>)
  124.  * rework the man page for improved clarity and completeness
  125.  * -----
  126.  * Internal
  127.  * -----
  128.  * avoid warning message from gcc on Sequent Balance mainframe
  129.  *    reported by Justin Sullivan <justin@f2.facts.uky.edu>
  130.  * bump max incoming line length to 2048 from 255
  131.  * add mcvert.ps target to Makefile
  132.  * -----
  133.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  134.  * (503) 685-1576 (work)
  135.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  136.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  137.  * Version 1.83 03Aug92
  138.  * ----------------------------------------------------------------------------
  139.  
  140.  * ----------------------------------------------------------------------------
  141.  * External
  142.  * -----
  143.  * Found and fixed problem with byte ordering plaguing users of
  144.  *    Sequent's Balance running DYNIX, and DEC computers.
  145.  *    The error message looked something like
  146.  *       hqx_to_bin_fork: writing nnn too many bytes
  147.  * Avoid generating debugging info if not to be printed -- cut runtime in half!
  148.  * Generalize and incorporate -I (info) option processing provided by
  149.  *    Paul Franklin, Computer Enginnering, Univ. of Calif., Davis CA 95616
  150.  *    pdfranklin@ucdavis.edu.
  151.  * Added heuristic to avoid false matches in mail headers.
  152.  *    problem expertly characterized, solution beta tested, and subsequent
  153.  *    improvement suggested by "Jim (J.) Lattanzi" <lattanzi@bnr.ca>
  154.  *    so segmented comp.binaries.mac files (either multi-file or concatenated
  155.  *    single file) should now convert correctly.
  156.  * Added -H switch to disable heuristic.
  157.  * Document heuristic in man page.
  158.  * Fixed (long-standing) bug which precluded -p option from being recognized
  159.  *    and verified decompressing and unpacking of PIT files working.
  160.  *    Thanks to Dave Clemans for providing me with a version of PackIt.
  161.  * Add the version to the extened Usage message emitted by the program.
  162.  * Tune the syntax of the summary in the program and man page.
  163.  * Cleaned up spelling mistakes in the man page.
  164.  * -----
  165.  * Internal
  166.  * -----
  167.  * Close all open streams --
  168.  *    fix for binfile by Paul Franklin <pdfranklin@ucdavis.edu>
  169.  * Incorporate changes suggested by Barry_Wolman@transarc.com
  170.  *    to mactypes.h and Makefile for support of IBM RS/6000 running AIX 3.2
  171.  *    reformat Makefile to avoid long option lines
  172.  * Identify the right Makefile lines for Irix too
  173.  *    suggested by Jack Repenning (jackr@dblues.wpd.sgi.com)
  174.  * Clean up the stream handling and add mopen/mclose
  175.  *    avoid unnecessary /dev/null opens
  176.  *    all file open/close/read/write are checked for success
  177.  * Lower lint content on SunOS and HP-UX.
  178.  *    avoiding all "sometimes ignored" lint messages.
  179.  * Improve modularity with mopen/mclose/converting routines.
  180.  * Tune debugging output information.
  181.  * Verify that passes smoke tests on DomainOS/SunOS/HP-UX/ULTRIX.
  182.  * Reformat these comments to avoid tabs.
  183.  * -----
  184.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  185.  * (503) 685-1576 (work)
  186.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  187.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  188.  * Version 1.87 25Sep92
  189.  * ----------------------------------------------------------------------------
  190.  
  191.  * ----------------------------------------------------------------------------
  192.  * External
  193.  * -----
  194.  * Add README file which describes how to configure and compile mcvert
  195.  * Handle multiple BinHex4.0 inputs in a single file again (thanks to
  196.  *    <Mark_Larimer@pigeon.cpg.cdc.com> for pointing out this regression)
  197.  * Emit the MacBinary header if verbose (to get create and modify times)
  198.  * -----
  199.  * Internal
  200.  * -----
  201.  * Rename some variables, create mac2unix (time) routine, more comments
  202.  * Keep lint content low
  203.  * Pull unnecessary include of <net/nh.h> -- avoid breaking AIX 3.1
  204.  * Fix = vs == typo dealing with protect bit
  205.  * -----
  206.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  207.  * (503) 685-1576 (work)
  208.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  209.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  210.  * Version 1.88 08Dec92
  211.  * ----------------------------------------------------------------------------
  212.  
  213.  * ----------------------------------------------------------------------------
  214.  * External
  215.  * -----
  216.  * emit input file names when verbose is enabled
  217.  *    (suggested by franklin@eecs.ucdavis.edu)
  218.  * make it easier to build on AT&T 3B2's
  219.  * -----
  220.  * Internal
  221.  * -----
  222.  * provide compile time switch to avoid bzero and bcopy, and use memset
  223.  *    and memcpy instead (pointed out by linger@drystone.attmail.com, and
  224.  *    requested again by Larry S. Staples <attjp4!lss>)
  225.  * update Makefile to include incantations required for AT&T 3B2's
  226.  * fflush all diagnostic output to ensure correct order when output to a file
  227.  * -----
  228.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  229.  * (503) 685-1576 (work)
  230.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  231.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  232.  * Version 1.89 05Jan93
  233.  * ----------------------------------------------------------------------------
  234.  
  235.  * ----------------------------------------------------------------------------
  236.  * External
  237.  * -----
  238.  * incorporate MAC_FILETYPE support provided by <root@genome.stanford.edu>
  239.  * minor edits to man page
  240.  * -----
  241.  * Internal
  242.  * -----
  243.  * update Makefile to simplify incantations required for AT&T 3B2's
  244.  * -----
  245.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  246.  * (503) 685-1576 (work)
  247.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  248.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  249.  * Version 1.90 04Mar93
  250.  * ----------------------------------------------------------------------------
  251.  
  252.  * ----------------------------------------------------------------------------
  253.  * External
  254.  * -----
  255.  * handle -b (both .data and .rsrc at same time) option
  256.  *    ( -> MacBinary suggested by bb@math.ufl.edu)
  257.  * remove anomalous file extension handling
  258.  *    (suggested by bb@math.ufl.edu)
  259.  * regularize MAC_EDITOR (author) and MAC_FILETYPE (file type) handling
  260.  * detect and report important file format errors
  261.  * emit output file name too
  262.  * revise Usage line
  263.  * massively revise man page to reflect changes and generally overhaul
  264.  * -----
  265.  * Internal
  266.  * -----
  267.  * avoid overwriting internal storage (what a chore!)
  268.  *    for example, mcvert -UI *.hqx used to abort with a segmentation violation
  269.  *    symptom reported by franklin@eecs.ucdavis.edu  Thu Sep 24 16:39:21 1992
  270.  * check return values from all getc/putc operations
  271.  * find and fix ancient bug extracting resource fork from MacBinary format
  272.  * identify failing file in EOF error messages
  273.  * clarify and amend distribution and update restrictions
  274.  * expand disclaimer (patterned after INFO-MAC CD-ROM -- Thx, Cliff and Joe!)
  275.  * -----
  276.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  277.  * (503) 685-1576 (work)
  278.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  279.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  280.  * Version 2.00 28Feb93
  281.  * ----------------------------------------------------------------------------
  282.  
  283.  * ----------------------------------------------------------------------------
  284.  * External
  285.  * -----
  286.  * unixify all relevent output files
  287.  *    (replace suspect characters with _)
  288.  * macify all relevent MacBinary files
  289.  *    (replace suspect chars with -, take first 20 and last 11 if > 31 char)
  290.  *    avoiding Mac file name > 31 chars (Rick Zaccone zaccone@bucknell.edu)
  291.  * always emit the Macintosh file name in the converting messages
  292.  *    since the UNIX file names are now provided by default
  293.  * rework man page to bring it more up to date, added OTHER SOURCES section
  294.  * emit data and rsrc len when printing bin header
  295.  * report the input character, not the mapped character, if avail,
  296.  *    else report mapped value as hex
  297.  * add -VV (Very Verbose) option
  298.  * -----
  299.  * Internal
  300.  * -----
  301.  * distribute mcvert.idraw, a postscript file
  302.  *    describing mcvert options and transformations pictorially,
  303.  *    contributed by Brian Bartholomew - bb@math.ufl.edu
  304.  * create and distribute README-conversion file
  305.  * update Makefile and README to make it more obvious how to build mcvert
  306.  *    problem reported by David Micklethwaite <mickles@cherax.super.csiro.au>
  307.  * make s/S/v/V flag processing serially reusable
  308.  * avoid obsolete ftime on HP-UX, SunOS, DomainOS -- default is now -DTIMEVAL
  309.  *    problem reported by smith@sfu.ca (Richard Smith) and
  310.  *    Adam Harris (harris@cs.uchicago.edu)
  311.  * avoid SGI bug regarding unterminated character constant within #ifdef notdef
  312.  *    problem reported by smith@sfu.ca (Richard Smith)
  313.  * clean up some FILE confusion
  314.  * make the man page work well across platforms
  315.  * re-lint on SunOS and HP-UX
  316.  * add various additional comments
  317.  * -----
  318.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  319.  * (503) 685-1576 (work)
  320.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  321.  * Version 2.09 30Jun93
  322.  * ----------------------------------------------------------------------------
  323.  
  324.  * ----------------------------------------------------------------------------
  325.  * External
  326.  * -----
  327.  * -----
  328.  * Internal
  329.  * -----
  330.  * incorporate SCO UNIX requirements into the Makefile
  331.  *    info from Fred Lenk, Camarillo, CA [fredgl@tecnet1.jcte.jcs.mil]
  332.  * backout intermediate XOBJ cleanup and continue to do what works for AT&T 3B2
  333.  *    belated & current thanks to Larry S. Staples [attjpn!lss@attibr.att.com]
  334.  *    for providing and proofing the working recipe for AT&T 3B2
  335.  * change OTHER SOURCES to OTHER PROGRAMS in man page, and mention programs
  336.  *    which run on the Mac, including CompactPro, StuffIt, and BinHex 4.0
  337.  * ship the formatted ASCII version of the man page for those without nroff
  338.  * -----
  339.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  340.  * (503) 685-1576 (work)
  341.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  342.  * Version 2.12 19Jul93
  343.  * ----------------------------------------------------------------------------
  344.  
  345.  * ----------------------------------------------------------------------------
  346.  * External
  347.  * -----
  348.  * -----
  349.  * Internal
  350.  * -----
  351.  * add StuffIt Expander mention to man page
  352.  * suggest using text (-t|-u) if data is text in the data (-d) description
  353.  * incorporate AIX Makefile improvement provided by
  354.  *    DaviD W. Sanderson (dws@ssec.wisc.edu)
  355.  * -----
  356.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  357.  * (503) 685-1576 (work)
  358.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  359.  * Version 2.13 13Sep93
  360.  * ----------------------------------------------------------------------------
  361.  
  362.  * ----------------------------------------------------------------------------
  363.  * External
  364.  * -----
  365.  * add -P (pipe to stdout) option
  366.  *    capability requested by lentz@rossi.astro.nwu.edu (Robert Lentz)
  367.  * have all info messages go to stderr, not stdout, to faciliate -P
  368.  *    (undo converting msg to stdout from version 1.6.5, dated Oct 1991)
  369.  * update man page to indicate changes
  370.  * -----
  371.  * Internal
  372.  * -----
  373.  * fiddle with info message to make it a bit clearer
  374.  * -----
  375.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  376.  * (503) 685-1576 (work)
  377.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  378.  * Version 2.14 10Nov93
  379.  * ----------------------------------------------------------------------------
  380.  
  381.  * ----------------------------------------------------------------------------
  382.  * External
  383.  * -----
  384.  * -----
  385.  * Internal
  386.  * -----
  387.  * ensure all source line lengths are less than 80
  388.  *    after someone or something wrapped the 2.14 archive, and it
  389.  *    would not compile (split a character string literal).
  390.  *    Thanks to Robert Hagopian (rhagopia@terminator.rs.itd.umich.edu)
  391.  *    and to L L Campbell (campbell@brahms.udel.edu) for bringing this
  392.  *    problem to my attention.
  393.  *    [recall that shar usually prepends X, which bumps the line length by 1]
  394.  *    [also, < 80 makes editing with emacs at 80 columns a bit better]
  395.  * add check_linelen to Makefile to help ensure compliance
  396.  * -----
  397.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  398.  * (503) 685-1576 (work)
  399.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  400.  * Version 2.15 15Nov93
  401.  * ----------------------------------------------------------------------------
  402.  
  403.  * ----------------------------------------------------------------------------
  404.  * External
  405.  * -----
  406.  * -----
  407.  * Internal
  408.  * -----
  409.  * fix original bug of failing to convert lengths when extracting data
  410.  *    and resource forks from MacBinary files -- thanks to Thomas Lange
  411.  *    <tlange@namu01.gwdg.de> for finding and supplying the fix for this bug
  412.  * tidy up for Alpha OSF1 -- thanks to Holger Debelts
  413.  *    <Debelts@rrz.Uni-Koeln.DE> for providing a fix for time()'s redeclaration
  414.  * check <= 80 on *archive*, and avoid always rebuilding shar
  415.  *    due to unfulfilled check_linelen dependency
  416.  * improve 2.15 comments
  417.  * toss uninteresting lint messages
  418.  * -----
  419.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  420.  * (503) 685-1576 (work)
  421.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  422.  * Version 2.16 14Oct94
  423.  * ----------------------------------------------------------------------------
  424.  
  425.  * ----------------------------------------------------------------------------
  426.  * This program may be freely distributed for non-profit purposes.  It
  427.  * may not be sold, by itself or as part of a collection of software.
  428.  * However, it may be distributed in source form with large
  429.  * collections of freeware and shareware, such as the INFO-MAC CD-ROM,
  430.  * which charge only a modest fee for publishing, but not selling, the
  431.  * software.  It may be freely modified as long as no modified version
  432.  * is independently distributed.  Modifications of interest to all can
  433.  * be incorporated into the program by sending them to me for
  434.  * inclusion and redistribution, or by releasing an updated mcvert to
  435.  * the info-mac archives.  Parts of the code can be used in other
  436.  * programs.  We hope you find mcvert useful, and enjoy using it.
  437.  * ----------------------------------------------------------------------------
  438.  
  439.  * ----------------------------------------------------------------------------
  440.  * DISCLAIMER -- USE mcvert software AT YOUR OWN RISK
  441.  * This mcvert software is provided "as is" without warrantee of any
  442.  * kind.  The entire risk as to the results and performance of the
  443.  * software is assumed by you, and in no event will we be liable for
  444.  * any consequential, incidental, or indirect damages suffered in the
  445.  * course of using this software.
  446.  * ----------------------------------------------------------------------------
  447.  
  448.  * ----------------------------------------------------------------------------
  449.  * Things that yet could be done:
  450.  * ---
  451.  * handle incoming BinHex4.0 files with ^M instead of ^J
  452.  * (requested by jonathan brecher brecher@husc.harvard.edu 29Mar93)
  453.  *  eg, right now we toss the entire line if it doesn't start with a colon
  454.  *  but the start of the line could be later on, following a ^M
  455.  * check for more file format errors, eg, MacBinary must be multiple of 128
  456.  * option to avoid .text extension on write (Rick Zaccone zaccone@bucknell.edu)
  457.  * check return values from fputs/fprintf
  458.  * provide header heuristic tuning option: set length and/or same sensitivity
  459.  * ----------------------------------------------------------------------------
  460.  */
  461.  
  462. /*
  463.  * Naming
  464.  *        DOWNLOAD
  465.  *            => converting TO MacBinary
  466.  *            => direction == FORWARDS
  467.  *            => use un_* routines (un => UNdo encoding?)
  468.  *        UPLOAD
  469.  *            => converting FROM MacBinary
  470.  *            => direction == BACKWARDS
  471.  *            => use re_* routines (re => Really Encode?)
  472.  */
  473.  
  474. #include "mactypes.h"
  475.  
  476. /* it would be natural to use an enum here, but avoid "fancy" features */
  477. #define HQX 0
  478. #define TEXT 1
  479. #define DATA 2
  480. #define RSRC 3
  481. #define BOTH 5
  482.  
  483. #define FORWARDS 0
  484. #define BACKWARDS 1
  485.  
  486. FILE *devnull;
  487. FILE *convert;
  488. FILE *verbose;
  489. FILE *debug;
  490. int   Debug;
  491.  
  492. char **hqxnames, **hqxnames_left;
  493. char *dir, *ext, *mac_auth, *mac_type;
  494. int translate_eol;
  495. char *maxlines_str;
  496. int maxlines;
  497.  
  498. /* used to skip suspect mail header lines */
  499. int suspect_shorter = 12;
  500. int suspect_same = 1;
  501.  
  502. /* used to avoid writing output files */
  503. int info_only;
  504.  
  505. /* pipe appropriate output -- write to stdout */
  506. int pipe_out;
  507.  
  508. char Usage[] = "\
  509. Usage: %s { [option] ... name ...} ...\n\
  510.  version:\t%4.2f\n\
  511.  default:\t-xDqv\n\
  512. \n\
  513.  option:\n\
  514. \t-x\tBinHex        .hqx  <-> MacBinary\n\
  515. \t-u\tText(trans)   .text <-> MacBinary\n\
  516. \t-h\tHost(as is)   .text <-> MacBinary\n\
  517. \t-d\tData          .data <-> MacBinary\n\
  518. \t-r\tResource      .rsrc <-> MacBinary\n\
  519. \t-b\tBoth    .data .rsrc <-> MacBinary\n\
  520. \n\
  521. \t-D\tDownload       Other -> MacBinary\n\
  522. \t-U\tUpload     MacBinary -> Other\n\
  523. \n\
  524. \t-p\tBinHex -> MacBinary => unpack PIT\n\
  525. \t-q\tdisable unpack PIT\n\
  526. \t-t\ttranslate end-of-line chars (useful with -b)\n\
  527. \n\
  528. \t-I\tInformation only (does not write output files)\n\
  529. \t-P\tPipe output to stdout\n\
  530. \t-s\tsilent\n\
  531. \t-S\tSilent about ``Converting ... '' lines too\n\
  532. \t-v\tverbose\n\
  533. \t-V\tVerbose, includes debugging information\n\
  534. \t-VV\tVery Verbose, includes extra debugging information\n\
  535. \t-H\tdisable skip-legal-but-suspect-lines Heuristic\n\
  536. \n\
  537. Environment:\n\
  538. \tMAC_FILETYPE  \tTEXT|????\tMac file type for Text|other\n\
  539. \tMAC_EDITOR    \tMACA|????\tMac creator (author) for Text|other\n\
  540. \tMAC_EXT       \t.bin     \textension for -D\n\
  541. \tMAC_DLOAD_DIR \t.        \tdirectory for -D\n\
  542. \tMAC_LINE_LIMIT\tnone     \tmaximum line length for -Ux\n\
  543. ";
  544.  
  545. char *cmdname;
  546.  
  547. main(argc, argv)
  548.     int argc;
  549.     char **argv;
  550. {
  551.     char *flags, *getenv();
  552.     int direction, mode, unpit_flag;
  553.  
  554.     cmdname = argv[0];
  555.  
  556.     /* Early error and clean exit if missing arguments */
  557.     if (argc < 2) {
  558.         usage();
  559.         /*NOTREACHED*/
  560.     }
  561.  
  562.     devnull = fopen("/dev/null", "w+");
  563.  
  564.     argv++;
  565.     argc--;
  566.  
  567.     convert = stderr;
  568.     verbose = stderr;
  569.     debug = devnull;
  570.     Debug = 0;
  571.  
  572.     direction = FORWARDS;
  573.     mode = HQX;
  574.     unpit_flag = 0;
  575.  
  576.     mac_type = getenv("MAC_FILETYPE");
  577.     mac_auth = getenv("MAC_EDITOR");
  578.  
  579.     if ((ext = getenv("MAC_EXT")) == NULL)
  580.         ext = ".bin";
  581.     if ((dir = getenv("MAC_DLOAD_DIR")) == NULL)
  582.         dir = ".";
  583.     if ((maxlines_str = getenv("MAC_LINE_LIMIT")) == NULL)
  584.         maxlines = 0;
  585.     else {
  586.         maxlines = atoi(maxlines_str);
  587.         if (maxlines < MIN_HQX_LINES) {
  588.             fprintf(stderr, "%s: %s; was %d; reset to %d\n",
  589.                 cmdname,
  590.                 "warning: MAC_LINE_LIMIT too small",
  591.                 maxlines, MIN_HQX_LINES);
  592.             fflush(stderr);
  593.             maxlines = MIN_HQX_LINES;
  594.         }
  595.     }
  596.  
  597.     /* Make command line arguments globally accessible */
  598.     hqxnames = (char **) calloc((unsigned)argc + 1, sizeof(char *));
  599.     hqxnames_left = hqxnames;
  600.     while (argc--)
  601.         *hqxnames_left++ = *argv++;
  602.  
  603.     /* Flag the end of the list */
  604.     *hqxnames_left = "-";
  605.     hqxnames_left = hqxnames;
  606.  
  607.     /* While not at the end of the list */
  608.     while (strcmp(*hqxnames_left, "-")) {
  609.         translate_eol = 0;
  610.         if (hqxnames_left[0][0] == '-') {
  611.             flags = *hqxnames_left++;
  612.             while (*++flags)
  613.                 switch (*flags) {
  614.                 case 'x':
  615.                     mode = HQX;
  616.                     break;
  617.                 case 'u':
  618.                     translate_eol = 1;
  619.                     mode = TEXT;
  620.                     break;
  621.                 case 'd':
  622.                     mode = DATA;
  623.                     break;
  624.                 case 'r':
  625.                     mode = RSRC;
  626.                     break;
  627.                 case 'h':
  628.                     translate_eol = 0;
  629.                     mode = TEXT;
  630.                     break;
  631.                 case 'b':
  632.                     mode = BOTH;
  633.                     break;
  634.                 case 't':
  635.                     translate_eol = 1;
  636.                     break;
  637.                 case 'D':
  638.                     direction = FORWARDS;
  639.                     break;
  640.                 case 'U':
  641.                     direction = BACKWARDS;
  642.                     break;
  643.                 case 'q':
  644.                     unpit_flag = 0;
  645.                     break;
  646.                 case 'p':
  647.                     unpit_flag = 1;
  648.                     break;
  649.                 case 'S':
  650.                     convert = devnull;
  651.                     verbose = devnull;
  652.                     debug = devnull;
  653.                     Debug = 0;
  654.                     break;
  655.                 case 's':
  656.                     convert = stderr;
  657.                     verbose = devnull;
  658.                     debug = devnull;
  659.                     Debug = 0;
  660.                     break;
  661.                 case 'v':
  662.                     convert = stderr;
  663.                     verbose = stderr;
  664.                     debug = devnull;
  665.                     Debug = 0;
  666.                     break;
  667.                 case 'V':
  668.                     convert = stderr;
  669.                     verbose = stderr;
  670.                     debug = stderr;
  671.                     Debug++;
  672.                     break;
  673.                 case'H':
  674.                     suspect_shorter = suspect_same = 0;
  675.                     break;
  676.                 case 'I':
  677.                     info_only = 1;
  678.                     break;
  679.                 case 'P':
  680.                     pipe_out = 1;
  681.                     break;
  682.                 default:
  683.                     usage();
  684.                     /*NOTREACHED*/
  685.                 }
  686.         }
  687.  
  688.         if (direction == BACKWARDS)
  689.             if (mode == HQX && unpit_flag)
  690.                 re_hqx();      /* no re_pit() yet */
  691.             else if (mode == HQX)
  692.                 re_hqx();
  693.             else
  694.                 re_other(mode);
  695.         else if (mode == HQX)
  696.             un_hqx(unpit_flag);
  697.         else
  698.             un_other(mode);
  699.     }
  700.  
  701.     exit(0);
  702.     /*NOTREACHED*/
  703. }
  704.  
  705. /* An array useful for CRC calculations that use 0x1021 as the "seed" */
  706. word magic[] = {
  707.     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  708.     0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  709.     0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  710.     0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  711.     0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  712.     0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  713.     0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  714.     0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  715.     0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  716.     0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  717.     0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  718.     0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  719.     0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  720.     0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  721.     0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  722.     0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  723.     0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  724.     0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  725.     0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  726.     0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  727.     0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  728.     0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  729.     0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  730.     0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  731.     0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  732.     0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  733.     0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  734.     0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  735.     0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  736.     0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  737.     0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  738.     0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  739. };
  740.  
  741.  
  742. /*
  743.  * calc_crc() --
  744.  *   Compute the MacBinary II-style CRC for the data pointed to by p, with the
  745.  *   crc seeded to seed.
  746.  *
  747.  *   Modified by Jim Van Verth to use the magic array for efficiency.
  748.  */
  749. short
  750. calc_mb_crc(p, len, seed)
  751.     unsigned char *p;
  752.     long len;
  753.     short seed;
  754. {
  755.     short hold;        /* crc computed so far */
  756.     long i;            /* index into data */
  757.  
  758.     extern unsigned short magic[];    /* the magic array */
  759.  
  760.     hold = seed;                   /* start with seed */
  761.     for (i = 0; i < len; i++, p++) {
  762.         hold ^= (*p << 8);
  763.         hold = (hold << 8) ^ magic[(unsigned char) (hold >> 8)];
  764.     }
  765.  
  766.     return (hold);
  767. }                /* calc_crc() */
  768.  
  769.  
  770. /* Report a fatal error, and exit, never return */
  771. error(msg, name)
  772.     char msg[], name[];
  773.  
  774. {
  775.     fprintf(stderr, msg, name);
  776.     (void)putc('\n', stderr);
  777.     perror("\nlast perror (may not be relevant)");
  778.     fprintf(stderr, "%s: exiting\n", cmdname);
  779.     if (debug == stderr)
  780.         abort();
  781.     exit(2);
  782.     /*NOTREACHED*/
  783. }
  784.  
  785. /* replace illegal Unix characters in file name */
  786. /* make sure host file name doesn't get truncated beyond recognition */
  787. unixify(np)
  788.     register char *np;
  789. {
  790.     register ulong c;
  791.  
  792.     c = strlen(np);
  793.     if (c > SYSNAMELEN - MAXEXTENSION)
  794.         c = SYSNAMELEN - MAXEXTENSION;
  795.     np[c] = '\0';
  796.  
  797.     /* pre-decrement to match pre-increment within loop */
  798.     np--;
  799.     while (c = *++np)
  800.         /*
  801.          * that is, ``if control or blank, or slash, or delete or 8bit''
  802.          * which is the same as ``if blank, slash, or non graphic''
  803.         */
  804.         if (c <= ' ' || c == '/' || c > '~')
  805.             *np = '_';
  806. }
  807.  
  808. /*
  809.  * replace illegal Macintosh characters in file name
  810.  * return resulting length
  811.  *
  812.  * According to Inside Macintosh, IV-90, valid file names
  813.  *    must be [1..31] characters long
  814.  *    must not contain a colon
  815.  *    must contain only printing characters
  816.  */
  817. macify(name, len, translate)
  818.     char *name;
  819.     int len;
  820.     int translate;
  821. {
  822.     register char *np;
  823.     register ulong c;
  824.     char *s, *t;
  825.     char buffer[SYSNAMELEN];
  826.  
  827.     /* make a copy to ensure null terminated */
  828.     strncpy(buffer, name, len);
  829.     buffer[len] = 0;
  830.     np = buffer;
  831.  
  832.     if (len < 1)
  833.         error("incoming file name is null", "");
  834.     if (len > 31) {
  835.         /* too long, so just take first 20 and last 11 */
  836.         s = np + 20;
  837.         t = np + len - 11;
  838.         while (*s++ = *t++)
  839.             ;
  840.         len = 31;
  841.     }
  842.  
  843.     if (translate) {
  844.         /* pre-decrement to match pre-increment within loop */
  845.         np--;
  846.         while (c = *++np)
  847.  
  848.          /*
  849.  
  850.           * Inside Macintosh, I-246, defines the printable characters
  851.           * for the Macintosh as 0x20 thru 0xD8 less 0x7F.  Yet, the
  852.           * apple character is above this range, at 0xF0, and the diamond
  853.           * character is below this range, at 0x13.  And Adobe Garamond
  854.           * has lots of characgters above 0xD8.
  855.           * 
  856.           * On the other hand, we only translate when processing UNIX
  857.           * file names which are usually ASCII, and ASCII printables are
  858.           * ' ' <= valid <= '~'.
  859.           * 
  860.           * But we want to avoid zapping any characters with the high
  861.           * order bit set, so 8 bit character users are not zinged.  I've
  862.           * never used a SONY-NeWS box, but this one's for you.  But how
  863.           * do we know if/that the UNIX and Macintosh extended characters
  864.           * are the same?
  865.           * 
  866.           * So what to do, what to do?
  867.           * 
  868.           * Let's look at it this way: if the UNIX file name has extended
  869.           * characters in it, they got there for a reason, hopefully on
  870.           * purpose, and we'll not gratuitously modify them.
  871.           * 
  872.           * And it looks like the 6.1.5 Finder running with the 6.0.5
  873.           * System will translate both control characters and colon into
  874.           * a dash, but leave the others alone, so so will we.
  875.           * 
  876.           * I don't know if MacOS, as opposed to the Finder, behaves
  877.           * differently, and it's too late tonight to find out.  Maybe
  878.           * some other time.
  879.  
  880.       */
  881.  
  882.           if (c < ' ' || c == ':' || c == '\177')
  883.                  *np = '-';
  884.     }
  885.  
  886.     /* copy the resulting string back in place */
  887.     strncpy(name, buffer, len);
  888.  
  889.     return len;
  890. }
  891.  
  892. /*
  893.  * Unix time (GMT since 1-1-1970)
  894.  * Mac time (local since 1-1-1904)
  895.  */
  896. #define MACTIMEDIFF 0x7c25b080    /* Mac time of 00:00:00 GMT, Jan 1, 1970 */
  897.  
  898. /* Convert Unix time to Mac time */
  899. ulong
  900. unix2mac(xtime)
  901.     ulong xtime;
  902. {
  903. #ifdef __PPC__
  904.  
  905.            return long2mac(xtime + MACTIMEDIFF);
  906.  
  907. #else /* __PPC__ */
  908.  
  909. #ifdef TIMEVAL
  910.     struct timeval t;
  911.     struct timezone tz;
  912.  
  913.     gettimeofday(&t, &tz);
  914.     return long2mac(xtime + MACTIMEDIFF
  915.        - 60 * (tz.tz_minuteswest - 60 * tz.tz_dsttime));
  916. #else
  917.     struct timeb tp;
  918.  
  919.     ftime(&tp);
  920.     return long2mac(xtime + MACTIMEDIFF
  921.         - 60 * (tp.timezone - 60 * tp.dstflag));
  922. #endif
  923.  
  924. #endif /* __PPC__ */
  925. }
  926.  
  927. /* Convert Mac time to Unix time */
  928. ulong
  929. mac2unix(xtime)
  930.     ulong xtime;
  931. {
  932. #ifdef __PPC__
  933.  
  934.     return (mac2long(xtime) - MACTIMEDIFF);
  935.  
  936. #else /* __PPC__ */
  937.  
  938. #ifdef TIMEVAL
  939.     struct timeval t;
  940.     struct timezone tz;
  941.  
  942.     gettimeofday(&t, &tz);
  943.     return (mac2long(xtime) - MACTIMEDIFF
  944.        + 60 * (tz.tz_minuteswest - 60 * tz.tz_dsttime));
  945. #else
  946.     struct timeb tp;
  947.  
  948.     ftime(&tp);
  949.     return (mac2long(xtime) - MACTIMEDIFF
  950.         + 60 * (tp.timezone - 60 * tp.dstflag));
  951. #endif
  952.  
  953. #endif /* __PPC__ */
  954. }
  955.  
  956. /*
  957.  * computes the appropriate output files
  958.  * and the appropriate optional suffixes,
  959.  * all depending on the processing "mode"
  960.  */
  961.  
  962. mode_to_fname_suffix(
  963.     mode, base_fname, data_fname, data_suffix, rsrc_fname, rsrc_suffix
  964. )
  965.     int mode;
  966.     char *base_fname, *data_fname, **data_suffix, *rsrc_fname, **rsrc_suffix;
  967. {
  968.  
  969.     /* clear names to indicate nothing selected yet */
  970.     *data_fname = *rsrc_fname = 0;
  971.  
  972.     switch (mode) {
  973.     case TEXT:
  974.         strcpy(data_fname, base_fname);
  975.         *data_suffix = ".text";
  976.         break;
  977.     case DATA:
  978.         strcpy(data_fname, base_fname);
  979.         *data_suffix = ".data";
  980.         break;
  981.     case RSRC:
  982.         strcpy(rsrc_fname, base_fname);
  983.         *rsrc_suffix = ".rsrc";
  984.         break;
  985.     case BOTH:
  986.         strcpy(data_fname, base_fname);
  987.         strcat(data_fname, ".data");
  988.         *data_suffix = "";
  989.         strcpy(rsrc_fname, base_fname);
  990.         strcat(rsrc_fname, ".rsrc");
  991.         *rsrc_suffix = "";
  992.         break;
  993.     default:
  994.         error("Internal error: unexpected mode", "");
  995.         break;
  996.     }
  997. }
  998.  
  999. /*
  1000.     This procedure basically copies the input file(s) to the output
  1001.     MacBinary file; in TEXT (translate_eol) mode it changes LF's to
  1002.     CR's, and in any mode it forges a Mac info header.  Author type
  1003.     comes from the MAC_EDITOR environment variable if it is defined.
  1004.  */
  1005.  
  1006. un_other(mode)
  1007.     int mode;
  1008. {
  1009.     char data_fname[SYSNAMELEN], rsrc_fname[SYSNAMELEN], binfname[SYSNAMELEN];
  1010.     FILE *data_file, *rsrc_file, *binfile;
  1011.     char *base_fname, *data_suffix, *rsrc_suffix;
  1012.     struct stat data_stbuf, rsrc_stbuf;
  1013.     ulong dlen, rlen, mtim, ctim;
  1014.  
  1015.     info_header info;
  1016.     register ulong b;                /* not character, must hold EOF diagnostic */
  1017.     register ulong nchars;
  1018.     int extra_chars;
  1019.     short crc;
  1020.     long len;
  1021.  
  1022.     while (hqxnames_left[0][0] != '-') {
  1023.  
  1024.         if (strlen(*hqxnames_left) >= SYSNAMELEN)
  1025.             error("Error: specified base file name is too long", "");
  1026.         base_fname = *hqxnames_left++;
  1027.  
  1028.         /* set up file names */
  1029.         mode_to_fname_suffix(mode, base_fname,
  1030.             data_fname, &data_suffix, rsrc_fname, &rsrc_suffix);
  1031.  
  1032.         /* process the data file, if requested */
  1033.         dlen = 0;
  1034.         if (*data_fname) {
  1035.             data_file = mopen(data_fname, data_suffix, "r");
  1036.             if (fstat(fileno(data_file), &data_stbuf))
  1037.                 error("Cannot stat %s", data_fname);
  1038.             mtim = unix2mac((ulong)data_stbuf.st_mtime);
  1039.             ctim = unix2mac((ulong)data_stbuf.st_ctime);
  1040.             dlen = long2mac(data_stbuf.st_size);
  1041.         }
  1042.  
  1043.         /* process the rsrc file, if requested */
  1044.         rlen = 0;
  1045.         if (*rsrc_fname) {
  1046.             rsrc_file = mopen(rsrc_fname, rsrc_suffix, "r");
  1047.             if (fstat(fileno(rsrc_file), &rsrc_stbuf))
  1048.                 error("Cannot stat %s", rsrc_fname);
  1049.             mtim = unix2mac((ulong)rsrc_stbuf.st_mtime);
  1050.             ctim = unix2mac((ulong)rsrc_stbuf.st_ctime);
  1051.             rlen = long2mac(rsrc_stbuf.st_size);
  1052.         }
  1053.  
  1054.         /* stuff header data into the info header */
  1055.  
  1056.         bzero((char*)&info, sizeof(info_header));
  1057.  
  1058.         info.nlen = strlen(base_fname);
  1059.         info.nlen = (info.nlen > NAMELEN) ? NAMELEN : info.nlen;
  1060.         strncpy((char*)info.name, base_fname, (int)info.nlen);
  1061.  
  1062.         /* now make sure the resulting name is valid */
  1063.         info.nlen = macify((char*)info.name, (int)info.nlen, 1);
  1064.  
  1065.         info.uploadvers = '\201';
  1066.         info.readvers = '\201';
  1067.  
  1068.         bcopy((char*)&mtim, (char*)info.mtim, 4);
  1069.         bcopy((char*)&ctim, (char*)info.ctim, 4);
  1070.  
  1071.         bcopy((char*)&dlen, (char*)info.dlen, 4);
  1072.         bcopy((char*)&rlen, (char*)info.rlen, 4);
  1073.  
  1074.         switch (mode) {
  1075.         case TEXT:
  1076.             bcopy(mac_type ? mac_type : "TEXT", (char*)info.type, 4);
  1077.             bcopy(mac_auth ? mac_auth : "MACA", (char*)info.auth, 4);
  1078.             break;
  1079.         case DATA:
  1080.         case RSRC:
  1081.         case BOTH:
  1082.             bcopy(mac_type ? mac_type : "????", (char*)info.type, 4);
  1083.             bcopy(mac_auth ? mac_auth : "????", (char*)info.auth, 4);
  1084.             break;
  1085.         default:
  1086.             error("Internal error: unexpected mode", "");
  1087.             break;
  1088.         }
  1089.  
  1090.         /* calculate CRC */
  1091.         crc = calc_mb_crc((unsigned char*)&info, 124L, 0);
  1092.         info.crc[0] = (char) (crc >> 8);
  1093.         info.crc[1] = (char) crc;
  1094.  
  1095.         /* Create the .bin file and write the info to it */
  1096.  
  1097.         len = strlen(dir) + strlen(base_fname) + strlen(ext) + 1;
  1098.         if (len >= sizeof(binfname))
  1099.             error("Error: generated binfname would be too long", "");
  1100.         /*
  1101.          * base_fname does not need to be unixified --
  1102.          * was valid coming in
  1103.          */
  1104.         sprintf(binfname, "%s/%s%s", dir, base_fname, ext);
  1105.         binfile = mopen(binfname, "", "w");
  1106.  
  1107.         converting(info.name, (int)info.nlen, info.type, info.auth);
  1108.         print_bin_hdr("Creating", &info);
  1109.         if (1 != fwrite((char*)&info, sizeof(info), 1, binfile))
  1110.             error("fwrite failed on binfile", "");
  1111.  
  1112.         /* pump out the data portion */
  1113.         if (*data_fname) {
  1114.             nchars = data_stbuf.st_size;
  1115.             extra_chars = 127 - (nchars + 127) % 128;
  1116.  
  1117.             if (translate_eol)
  1118.                 while (nchars--) {
  1119.                     (b = getc(data_file)) == EOF &&
  1120.                         error("Error: getc failed on data_file", "");
  1121.                     if (b == LF)
  1122.                         b = CR;
  1123.                     putc((char)b, binfile) == EOF &&
  1124.                         error("Error: putc failed on binfile", "");
  1125.                 }
  1126.  
  1127.             else
  1128.                 while (nchars--) {
  1129.                     (b = getc(data_file)) == EOF &&
  1130.                         error("Error: getc failed on data_file", "");
  1131.                     putc((char)b, binfile) == EOF &&
  1132.                         error("Error: putc failed on binfile", "");
  1133.                 }
  1134.  
  1135.             while (extra_chars--) {
  1136.                 putc(0, binfile) == EOF &&
  1137.                     error("Error: putc failed on binfile", "");
  1138.             }
  1139.  
  1140.             mclose(&data_file, "txtfile");
  1141.         }
  1142.  
  1143.         /* pump out the rsrc portion */
  1144.         if (*rsrc_fname) {
  1145.             nchars = rsrc_stbuf.st_size;
  1146.             extra_chars = 127 - (nchars + 127) % 128;
  1147.  
  1148.             while (nchars--) {
  1149.                 (b = getc(rsrc_file)) == EOF &&
  1150.                     error("Error: getc failed on rsrc_file", "");
  1151.                 putc((char)b, binfile) == EOF &&
  1152.                     error("Error: putc failed on binfile", "");
  1153.             }
  1154.  
  1155.             while (extra_chars--) {
  1156.                 putc(0, binfile) == EOF &&
  1157.                     error("Error: putc failed on binfile", "");
  1158.             }
  1159.  
  1160.             mclose(&rsrc_file, "txtfile");
  1161.         }
  1162.  
  1163.         mclose(&binfile, "binfile");
  1164.     }
  1165. }
  1166.  
  1167. /*
  1168.     This procedure basically copies the MacBinary input file to the
  1169.     output file(s); in TEXT (translate_eol) mode it changes CR's to
  1170.     LF's, and in any mode it skikps over the Mac info header.
  1171.  */
  1172.  
  1173. re_other(mode)
  1174.     int mode;
  1175. {
  1176.     char base_fname[SYSNAMELEN];
  1177.     char data_fname[SYSNAMELEN], rsrc_fname[SYSNAMELEN], binfname[SYSNAMELEN];
  1178.     FILE *data_file, *rsrc_file, *binfile;
  1179.     char *data_suffix, *rsrc_suffix;
  1180.  
  1181.     info_header info;
  1182.     register ulong b;
  1183.     register ulong nchars;
  1184.     ulong temp;
  1185.     int extra_chars;
  1186.     long len;
  1187.  
  1188.     while (hqxnames_left[0][0] != '-') {
  1189.  
  1190.         /* suck in the MacBinary header */
  1191.         if (strlen(*hqxnames_left) >= sizeof(binfname))
  1192.             error("Error: specified binfname is too long", "");
  1193.         strcpy(binfname, *hqxnames_left++);
  1194.         binfile = mopen(binfname, ext, "r");
  1195.         if (1 != fread((char*)&info, sizeof(info), 1, binfile))
  1196.             error("fread failed on binfile", "");
  1197.  
  1198.         /* figure out the target base file name */
  1199.         if (info.nlen >= NAMELEN)
  1200.             error("Error: corrupt BinHex data format", "");
  1201.         strncpy(base_fname, (char*)info.name, (int)info.nlen);
  1202.         base_fname[info.nlen] = '\0';
  1203.         converting(info.name, (int)info.nlen, info.type, info.auth);
  1204.         print_bin_hdr("Reading", &info);
  1205.  
  1206.         /* ensure the output files have no bogus UNIX characters */
  1207.         unixify(base_fname);
  1208.         mode_to_fname_suffix(mode, base_fname,
  1209.             data_fname, &data_suffix, rsrc_fname, &rsrc_suffix);
  1210.  
  1211.         /* always use suffix on write */
  1212.  
  1213.         if (*data_fname) {
  1214.             len = strlen(data_fname) + strlen(data_suffix);
  1215.             if (len >= sizeof(data_fname))
  1216.                 error("Error: generated data_fname would be too long", "");
  1217.             strcat(data_fname, data_suffix);
  1218.             data_file = mopen(data_fname, "", "w");
  1219.         }
  1220.  
  1221.         if (*rsrc_fname) {
  1222.             len = strlen(rsrc_fname) + strlen(rsrc_suffix);
  1223.             if (len >= sizeof(rsrc_fname))
  1224.                 error("Error: generated rsrc_fname would be too long", "");
  1225.             strcat(rsrc_fname, rsrc_suffix);
  1226.             rsrc_file = mopen(rsrc_fname, "", "w");
  1227.         }
  1228.  
  1229.         /* process the data fork */
  1230.         bcopy((char*)info.dlen, (char *) &temp, 4);
  1231.         nchars = mac2long(temp);
  1232.         extra_chars = 127 - (nchars + 127) % 128;
  1233.  
  1234.         if (*data_fname) {
  1235.  
  1236.             if (translate_eol)
  1237.                 while (nchars--) {
  1238.                     (b = getc(binfile)) == EOF &&
  1239.                         error("Error: getc failed on binfile", "");
  1240.                     if (b == CR)
  1241.                         b = LF;
  1242.                     putc((char)b, data_file) == EOF &&
  1243.                         error("Error: putc failed on data_file", "");
  1244.                 }
  1245.  
  1246.             else
  1247.                 while (nchars--) {
  1248.                     (b = getc(binfile)) == EOF &&
  1249.                         error("Error: getc failed on binfile", "");
  1250.                     putc((char)b, data_file) == EOF &&
  1251.                         error("Error: putc failed on data_file", "");
  1252.                 }
  1253.         
  1254.             mclose(&data_file, data_fname);
  1255.  
  1256.         } else {
  1257.  
  1258.             /* skip the actual data */
  1259.             while (nchars--) {
  1260.                 getc(binfile) == EOF &&
  1261.                     error("Error: getc failed on binfile", "");
  1262.             }
  1263.         }
  1264.  
  1265.         /* eat the padding to 128 byte boundary */
  1266.         while (extra_chars--) {
  1267.             getc(binfile) == EOF &&
  1268.                 error("Error: getc failed on binfile", "");
  1269.         }
  1270.  
  1271.         /* process the rsrc fork */
  1272.  
  1273.         bcopy((char*)info.rlen, (char *) &temp, 4);
  1274.         nchars = mac2long(temp);
  1275.  
  1276.         if (*rsrc_fname) {
  1277.             while (nchars--) {
  1278.                 (b = getc(binfile)) == EOF &&
  1279.                     error("Error: getc failed on binfile", "");
  1280.                 putc((char)b, rsrc_file) == EOF &&
  1281.                     error("Error: putc failed on rsrc_file", "");
  1282.             }
  1283.             mclose(&rsrc_file, rsrc_fname);
  1284.         }
  1285.  
  1286.         mclose(&binfile, "binfile");
  1287.     }
  1288. }
  1289.  
  1290. usage()
  1291. {
  1292.     fprintf(stderr, Usage, cmdname, VERSION/100.);
  1293.     exit(1);
  1294.     /*NOTREACHED*/
  1295. }
  1296.  
  1297.  
  1298. /* My FileIO routines, to enable clean implementation of info_only */
  1299. /* If info_only and open for write, inform user and use devnull instead */
  1300. /* If pipe_out and open for write, inform user and use stdout instead */
  1301. /* If trying to close devnull or stdout, don't */
  1302.  
  1303. FILE *
  1304. mopen(name, exten, type)
  1305.     char *name;
  1306.     char *exten;
  1307.     char *type;
  1308. {
  1309.     FILE *result;
  1310.  
  1311.     switch (*type) {
  1312.     case 'r':
  1313.         result = fopen(name, type);
  1314.         if (result == NULL && *exten) {
  1315.             /* see if adding the extension would help */
  1316.             int len_root = strlen(name);
  1317.             int len_ext = strlen(exten);
  1318.             char *dotspot = name + len_root - len_ext;
  1319.             if (strcmp(exten, dotspot)) {
  1320.                 if (len_root + len_ext >= SYSNAMELEN)
  1321.                     error("Error: generated file name would be too long", "");
  1322.                 strcat(name, exten);
  1323.                 result = fopen(name, type);
  1324.             }
  1325.         }
  1326.         if (result == NULL)
  1327.             error("Cannot open %s for read", name);
  1328.         else {
  1329.             fprintf(verbose, "\n%-15s%-27s\n",
  1330.                 "Input file", name);
  1331.             fflush(verbose);
  1332.         }
  1333.         break;
  1334.  
  1335.     case 'w':
  1336.         if (info_only) {
  1337.             fprintf(verbose, " %-14s%-27s -I (info only) specified\n",
  1338.                 " No output to", name);
  1339.             fflush(verbose);
  1340.             result = devnull;
  1341.         } else if (pipe_out) {
  1342.             fprintf(verbose, " %-14s%-27s -P (pipe to stdout) specified\n",
  1343.                 " stdout, vice", name);
  1344.             fflush(verbose);
  1345.             result = stdout;
  1346.         } else {
  1347.             result = fopen(name, type);
  1348.             if (result == NULL)
  1349.                 error("Cannot open %s for write", name);
  1350.             else {
  1351.                 fprintf(verbose, "%-15s%-27s\n",
  1352.                     "Output file", name);
  1353.                 fflush(verbose);
  1354.             }
  1355.         }
  1356.         break;
  1357.  
  1358.     default:
  1359.         fprintf(stderr, "%s: internal error in mopen -- exiting\n", cmdname);
  1360.         exit(2);
  1361.  
  1362.     }
  1363.  
  1364.     return result;
  1365. }
  1366.  
  1367. mclose(stream_p, name)
  1368.     FILE **stream_p;
  1369.     char *name;
  1370. {
  1371.     if (*stream_p && *stream_p != devnull && *stream_p != stdout) {
  1372.         if (fclose(*stream_p) == EOF)
  1373.             error("Error closing %s", name);
  1374.     }
  1375.  
  1376.     *stream_p = (FILE *)0;
  1377. }
  1378.  
  1379. converting(name, len, type, auth)
  1380.     byte *name;
  1381.     int len;
  1382.     byte *type;
  1383.     byte *auth;
  1384. {
  1385.     char buffer[SYSNAMELEN];
  1386.  
  1387.     /* make a copy to ensure null terminated */
  1388.     strncpy(buffer, (char*)name, len);
  1389.     buffer[len] = 0;
  1390.  
  1391.     fprintf(convert,
  1392.         "%-15s%-30s type = \"%4.4s\", author = \"%4.4s\"\n",
  1393.          info_only ? "Inspecting" : "Converting",
  1394.          buffer, type, auth);
  1395.     fflush(convert);
  1396. }
  1397.  
  1398. print_bin_hdr(msg, hdr)
  1399.     char *msg;
  1400.     info_header *hdr;
  1401. {
  1402.     ulong otime, xtime;
  1403.     long    dlen, rlen;
  1404.     bcopy((char*)hdr->ctim, (char*)&otime, 4);
  1405.     DEBUG && fprintf(debug,
  1406.         "DEBUG: verifying mac2unix/unix2mac: 0x%8lx, 0x%8lx\n",
  1407.         otime, unix2mac(mac2unix(otime)));
  1408.  
  1409.     bcopy((char*)hdr->dlen, (char*)&dlen, 4);
  1410.     bcopy((char*)hdr->rlen, (char*)&rlen, 4);
  1411.  
  1412.     DEBUG && fprintf(debug, "\
  1413. %s\n\
  1414. \tName     %.*s\n\
  1415. \tType     %.4s\n\
  1416. \tCreator  %.4s\n\
  1417. \tDataLen  %ld\n\
  1418. \tRsrcLen  %ld\n\
  1419. ",
  1420.         msg,
  1421.         hdr->nlen, hdr->name,
  1422.         hdr->type,
  1423.         hdr->auth,
  1424.         mac2long(dlen),
  1425.         mac2long(rlen),
  1426.         0);
  1427.  
  1428.     bcopy((char*)hdr->ctim, (char*)&otime, 4);
  1429.     xtime = mac2unix(otime);
  1430.     DEBUG && fprintf(debug, "raw ctim: 0x%8lx, mac2unix: 0x%8lx\n",
  1431.         otime, xtime);
  1432.     DEBUG && fflush(debug);
  1433.  
  1434.     fprintf(verbose, "\tCreated  %s", ctime((time_t *)&xtime));
  1435.     fflush(verbose);
  1436.  
  1437.     bcopy((char*)hdr->mtim, (char*)&otime, 4);
  1438.     xtime = mac2unix(otime);
  1439.     DEBUG && fprintf(debug, "raw mtim: 0x%8lx, mac2unix: 0x%8lx\n",
  1440.         otime, xtime);
  1441.     DEBUG && fflush(debug);
  1442.     fprintf(verbose, "\tModified %s", ctime((time_t *)&xtime));
  1443.     fflush(verbose);
  1444.  
  1445. }
  1446.